<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\api\controller\v1\car;


use app\model\car\CarOrder;
use app\model\user\User;
use app\Request;
use app\services\car\CarContactServices;
use app\services\car\CarOrderServices;
use app\services\car\OrderPayServices;
use think\exception\ValidateException;

class CarContractController
{
    /**
     * 驾驶员资质提交
     * @param Request $request
     * @param CarContactServices $services
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function driverQualification(Request $request, CarContactServices $services)
    {
        $data = $request->postMore([
            ['id', 0],
            ['name', ''],
            ['card_no', ''],
            ['card_a', ''],
            ['card_b', ''],
            ['car_cert', ''],
            ['car_cert2', ''],
            ['phone', ''],
        ]);
        if (!$data['name']) return app('json')->fail('请输入驾驶员姓名');
        if (!$data['card_no']) return app('json')->fail('请输入驾驶员二代身份证');
        if (!check_card($data['card_no'])) return app('json')->fail('身份证号格式错误');
        if (!$data['card_a']) return app('json')->fail('请上传身份证正面图');
        if (!$data['card_b']) return app('json')->fail('请上传身份证反面图');
        if (!$data['car_cert']) return app('json')->fail('请上传驾驶证正页照片');
        if (!$data['car_cert2']) return app('json')->fail('请上传驾驶证副页照片');
        if (!$data['phone']) return app('json')->fail('请输入驾驶员手机号');
        if (!check_phone($data['phone'])) return app('json')->fail('手机号格式错误');
        $data['uid'] = $request->uid();
        //请求约你游验证身份证正面信息接口
        $url = 'http://apiv4.klch.cn/api/ImageUpdate/GetCardIdInfo?url='.$data['card_a'];
        $ret = doCurlGetRequest($url);
        $ret = json_decode($ret,true);
        if(!$ret['status'])return app('json')->fail('请上传正确清晰的身份证正面图');
        return app('json')->success($services->driverQualification((int)$data['id'], $data));
    }

    /**
     * 获取取车人信息
     * @param Request $request
     * @param CarContactServices $services
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCarContactList(Request $request, CarContactServices $services)
    {
        $uid = $request->uid();
        $list = $services->getList(['uid' => $uid,'is_del'=>0]);
        return app('json')->success($list);
    }

    /**
     * 获取一条信息
     * @param Request $request
     * @param CarContactServices $services
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getContactInfo(Request $request, CarContactServices $services)
    {
        $data = $request->getMore([
            ['contact_id', ''],
        ]);
        return app('json')->success($services->getContactInfo((int)$data['contact_id']));
    }
}
